var vehicleMonitor = {
    init : function(){
       mapTool.init();// 地图初始化
       
       this.vehicleTimer=null;       // 订阅定时器
       this.terminalMap = new HashMap();
       this.vehicleMap = new HashMap();
//       this.initVehicleList(); 
       this.myChart=null;
       this.time=[];
       this.speed=[];
       this.initEcharts();
       if(vehicleNo){
    	   this.subScribe();
   	   }else{
   		   global.ajaxLoading.pop("无车辆信息",true,2000);
   	   }
       this.bind();
       this.lineArr=[];
       this.currentPoint=[];
       this.lastPoint=[];
    },
   
    subScribe : function(commno){
    	 var CommNO=global.getUrlParam("commNo");
    	 if(commno){
    		 CommNO=commno;
    	 }
    	 if(CommNO){
    		 console.log();
    	 }else{
    		 global.ajaxLoading.pop("当前没有定位数据",true,2000);
    		 return;
    	 }
	   	 var getSubScribeData = function(CommNO){
	   		var getTimestamp=new Date().getTime();
	   		 $.ajax({
	                url : global.url.domain+'gps-web/gis/monitor/subScribe.html?v='+getTimestamp,
	                data :{"commnos":CommNO},
	                success : function(data){
	                    if(data&&data!=""){
	                    	var result = JSON.parse(data);
	                    	if(result.length>0){
	                    		mapTool.clearMarker(result[0].vehicleId);
	                    		result[0].vehicleNo=vehicleNo;
		                    	mapTool.addMarker(result[0]);
		                    	mapTool.setCenter(result[0]);
		                    	vehicleMonitor.drawTrack(result[0]);    	
		                    	vehicleMonitor.time.push(result[0].gpsTimeStr);
		                    	vehicleMonitor.speed.push(result[0].gpsSpeedF);
		                        vehicleMonitor.myChart.setOption({
		                            xAxis: {
		                                data: vehicleMonitor.time
		                            },
		                            series: [{
		                                name:'速度',
		                                data: vehicleMonitor.speed
		                            }]
		                        });
	                    	}else{
		                    	global.ajaxLoading.pop("当前车辆无定位数据",true,2000);
		                    }
	                    }else{
	                    	global.ajaxLoading.pop("当前车辆无定位数据",true,2000);
	                    }
	                }
	            });
	   	 };
	   	 getSubScribeData(CommNO);
	   	 clearInterval(vehicleMonitor.vehicleTimer);
	   	 if(CommNO){
	   		 vehicleMonitor.vehicleTimer = setInterval(function(){
	 	   		getSubScribeData(CommNO);          
	 		 },10000);
	   	 }else{
	   		//global.ajaxLoading.pop("当前车辆无定位数据",true,2000);
	   	 }
	   		
    },
    /**
     * 画轨迹
     * @param data
     */
    drawTrack : function(data){
    	vehicleMonitor.currentPoint=[data.longitude,data.latitude];
    	if(vehicleMonitor.currentPoint.length==2&&vehicleMonitor.lastPoint.length==2){
    		vehicleMonitor.lineArr.push(vehicleMonitor.currentPoint);
        	vehicleMonitor.lineArr.push(vehicleMonitor.lastPoint);
    	}
    	if(vehicleMonitor.lineArr.length==2){
    		mapTool.drawTrack(data,vehicleMonitor.lineArr);
    		vehicleMonitor.lineArr=[];
    	}
    	vehicleMonitor.lastPoint=vehicleMonitor.currentPoint;
    	vehicleMonitor.currentPoint=[];
    },
    initEcharts : function(){
    	vehicleMonitor.myChart = echarts.init(document.getElementById('speedCharts'));
    	var option = {
    		    xAxis: {
    		    	type : 'category',
    	            boundaryGap : false,
    		        data:vehicleMonitor.time 
    		    },
    		    grid:{
    		    	x:30,
    		    	y:30,
    		    	x2:5,
    		    	y2:5
    		    	
    		    },
    		    calculable : true,
    		    tooltip: {
    		        trigger: 'axis',
    		        formatter: function (params) {
    		            params = params[0];
    		            return "时间:"+params.name+"<br/>"+"速度"+params.value;
    		        },
    		        axisPointer: {
    		            animation: false
    		        }
    		    },
    		    yAxis: {
    		        name : '速度(km/h)',
    			    type : 'value',
    			    max : 150,
    			    data : [30,60,90,120,150]
    		    },
    		    series: [
    		        {
    		            name:'速度',
    		            type:'line',
    		            smooth:true,
    		            stack: 'a',
    		            data: [0]
    		        }
    		    ]
    		};
    	vehicleMonitor.myChart.setOption(option);
    },
    bind : function(){
    	$("#selectVehicleSel").on("change", function(e) { 
    		vehicleMonitor.currentPoint=[];
    		vehicleMonitor.lastPoint=[];
    		clearInterval(vehicleMonitor.vehicleTimer);  		
    		mapTool.clearAllMarker();
    		mapTool.clearAllPolyline();
    		var vehicleId = $("#selectVehicleSel").val();
    		vehicleMonitor.subScribe(vehicleMonitor.terminalMap.get(vehicleId));
    		
    		vehicleMonitor.time=[];
        	vehicleMonitor.speed=[];
    		vehicleMonitor.initEcharts();
    		vehicleMonitor.myChart.setOption({
                xAxis: {
                    data: []
                },
                series: [{
                    name:'速度',
                    data: [0]
                }]
            });
    	});
    }
};

vehicleMonitor.init();
